package com.slwish.xty.ui.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.slwish.common.base.PresenterToolbarActivity;
import com.slwish.common.utils.StringUtils;
import com.slwish.xty.R;
import com.slwish.xty.mvp.contract.ForgetPsdContract;
import com.slwish.xty.mvp.presenter.ForgetPsdPresenter;
import com.slwish.xty.utils.TimerUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class ForgetPsdActivity extends PresenterToolbarActivity<ForgetPsdContract.Presenter> implements ForgetPsdContract.View {
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_yzm)
    EditText mEtYzm;
    @BindView(R.id.et_password)
    EditText mEtPassword;
    @BindView(R.id.tv_send_sms)
    TextView mTvSendSms;
    @BindView(R.id.btn_login)
    TextView btnLogin;

    @Override
    protected int getLayoutId() {
        return R.layout.act_forget_psd;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle(R.string.reset_pass_text);
        mEtYzm.addTextChangedListener(textWatcher);
        mEtPassword.addTextChangedListener(textWatcher);
        mEtPhone.addTextChangedListener(textWatcher);
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {

            if (TextUtils.isEmpty(mEtPassword.getText()) ||
                    TextUtils.isEmpty(mEtPhone.getText()) || TextUtils.isEmpty(mEtYzm.getText())) {
                btnLogin
                        .setBackgroundResource(R.drawable.btn_unable_selector);
                btnLogin.setEnabled(false);
            } else {
                btnLogin
                        .setBackgroundResource(R.drawable.btn_select_selector);
                btnLogin.setEnabled(true);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    @OnClick({R.id.tv_send_sms, R.id.btn_login})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_send_sms:
                if (StringUtils.isEmpty(mEtPhone.getText().toString())) {
                    showToast(R.string.input_phone_number_hint);
                    return;
                }
                if (!StringUtils.isMobileExact(mEtPhone.getText().toString())) {
                    showToast(R.string.input_right_phone_number_hint);
                    return;
                }
//                CountTimerUtils mCountDownTimerUtils = new CountTimerUtils(mContext, mTvSendSms, 10000, 1000);
//                mCountDownTimerUtils.start();
                TimerUtils timerUtils = new TimerUtils(mContext.getApplicationContext(), mTvSendSms);
                timerUtils.RunTimer();
                mPresenter.getVerifyCode(mEtPhone.getText().toString(), "changePassword");
                break;

            case R.id.btn_login:
                mPresenter.resetPsd(mContext, mEtPhone.getText().toString(), mEtYzm.getText().toString(), mEtPassword.getText().toString());
                break;
        }

    }

    /**
     * 重置成功
     */
    @Override
    public void resetSuccess() {
        finish();
    }

    @Override
    protected ForgetPsdContract.Presenter initPresenter() {
        return new ForgetPsdPresenter(this);
    }
}
